package com.demo.dao;

import com.demo.dto.ProjectDto;
import com.demo.pojo.Contractor;
import com.demo.pojo.Developer;
import com.demo.pojo.Item;
import com.demo.pojo.Project;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName Project
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/25 10:51
 * @Version 1.0
 **/
public interface ProjectDao {

    @Select("select * from Project")
    public List<Project> findAll();


    //查询工程信息及其对应关系的数据
    @Select("select * from project where status=1")
    @Results(id = "allMap", value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "pname", property = "pname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "did", property = "did"),
            @Result(column = "cid", property = "cid"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "status", property = "status"),
//            一对多 （开发商部门）
            @Result(column = "pid", property = "branches",
                    many = @Many(select = "com.demo.dao.BranchDao.findBranchAndTaskByPid")
            ),
//            一对多（分项工程）
            @Result(column = "pid", property = "subitems",
                    many = @Many(select = "com.demo.dao.SubitemDao.findSubitemsAndItems")
            ),
//            一对多（工程阶段）
            @Result(column = "pid", property = "stages",
                    many = @Many(select = "com.demo.dao.StageDao.findStageByPid")
            ),
//            一对一（开发商）
            @Result(column = "did", property = "developer", javaType = Developer.class,
                    one = @One(select = "com.demo.dao.DeveloperDao.findDeveloperByDid")
            ),
//            一对一（承建商）
            @Result(column = "cid", property = "contractor", javaType = Contractor.class,
                    one = @One(select = "com.demo.dao.ContractorDao.findContractorByCid")
            )
    })
    public List<Project> findOneAndMany();

    @Select("select * from project where status=1")
    @Results(id = "allDtoMap", value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "pname", property = "pname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "did", property = "did"),
            @Result(column = "cid", property = "cid"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "status", property = "status"),
            @Result(column = "did", property = "developerName", javaType = String.class,
                    one = @One(select = "com.demo.dao.DeveloperDao.getNameByDid")
            ),
            @Result(column = "cid", property = "contractorName", javaType = String.class,
                    one = @One(select = "com.demo.dao.ContractorDao.getNameByCid")
            )
    })
    public List<ProjectDto> findAllDto();

    @Select("select * from project where pid=#{pid} and status=1")
    @ResultMap("allDtoMap")
    public ProjectDto findDtoById(int pid);


    @Select("select * from project where pid=#{pid} and status=1")
    public Project findOneById(int pid);


    @Update("update project set pname=#{pname},starttime=#{starttime},endtime=#{endtime} where pid=#{pid}")
    public int updateProject(Project project);

    @Update("update project set status=0 where pid=#{pid}")
    public  int  deleteProjectById(int pid);


    //插入数据
    @Insert("<script> insert into project(\n" +
            "            <trim suffixOverrides=\",\">\n" +
            "                    pid,\n" +
            "                <if test=\"pname!=null and pname!=''\">\n" +
            "                    pname,\n" +
            "                </if>\n" +
            "                <if test=\"starttime!=null and starttime!=''\">\n" +
            "                    starttime,\n" +
            "                </if>\n" +
            "                <if test=\"endtime!=null and endtime!=''\">\n" +
            "                    endtime,\n" +
            "                </if>\n" +
            "                <if test=\"did!=null and did!=''\">\n" +
            "                    did,\n" +
            "                </if>\n" +
            "                <if test=\"cid!=null and cid!=''\">\n" +
            "                    cid,\n" +
            "                </if>\n" +
            "                <if test=\"updatetime!=null and updatetime!=''\">\n" +
            "                    updatetime,\n" +
            "                </if>\n" +
            "            </trim>\n" +
            "        )\n" +
            "        values (\n" +
            "        <trim suffixOverrides=\",\">\n" +
            "                0,\n" +
            "            <if test=\"pname!=null and pname!=''\">\n" +
            "                #{pname},\n" +
            "            </if>\n" +
            "            <if test=\"starttime!=null and starttime!=''\">\n" +
            "                #{starttime},\n" +
            "            </if>\n" +
            "            <if test=\"endtime!=null and endtime!=''\">\n" +
            "               #{endtime} ,\n" +
            "            </if>\n" +
            "            <if test=\"did!=null and did!=''\">\n" +
            "               #{did} ,\n" +
            "            </if>\n" +
            "            <if test=\"cid!=null and cid!=''\">\n" +
            "               #{cid} ,\n" +
            "            </if>\n" +
            "            <if test=\"updatetime!=null and updatetime!=''\">\n" +
            "               #{updatetime} ,\n" +
            "            </if>\n" +
            "        </trim>\n" +
            "        )"+
            "</script>")
    int insertProject(Project project);
}
